package com.sanjiahantang.youjiarole.module.common.presenters;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;


import com.google.gson.Gson;
import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.common.beans.MessageBean;
import com.sanjiahantang.youjiarole.module.common.models.IUserModel;
import com.sanjiahantang.youjiarole.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiarole.module.common.views.MessageView;
import com.sanjiahantang.youjiarole.utils.L;
import com.sanjiahantang.youjiarole.utils.RelativeDateFormat;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import q.rorbin.badgeview.QBadgeView;


/**
 * Created by zy on 2017/9/5.
 */

public class MessagePresenter extends BaseMvpPresenter<MessageView> implements CallBack<MessageBean> {
    private IUserModel userModel;
    private QuickAdapter<MessageBean.Data> quickAdapter;
    private Gson gson;
    private List<MessageBean.Data> dataList;

    public MessagePresenter() {
        userModel = new UserModel();
        gson = new Gson();
        dataList = new ArrayList<>();
    }

    public void getMessage(Context context) {

        String rolePhone= SPUtils.get(context, SPConfig.PHONE,"").toString();
        String roleId= SPUtils.get(context, SPConfig.ROLE_ID,"").toString();
        Log.e("参数",rolePhone);
        Map<String,String> params=new HashMap<>();
        params.put("rolePhone",rolePhone);
        params.put("roleId",roleId);
        params.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        userModel.getUserMessage(context,params,this);
    }

    @Override
    public void onSuccess(MessageBean response, Context context, Object tag) {
        if ("200".equals(String.valueOf(response.getCode()))) {
            Log.e("返回", "" + response.getData());
//            List<MessageBean.Data> dataList=new ArrayList<>();
            try {
                MessageBean.ListData data = gson.fromJson(response.getData(), MessageBean.ListData.class);
                if (dataList.size() > 0) {
                    dataList.clear();
                }
                dataList.addAll(data.notReadList);
                dataList.addAll(data.readList);
                quickAdapter.clear();
                quickAdapter.addAll(dataList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {

        }
    }

    @Override
    public void onFailure(String message) {
        if (!TextUtils.isEmpty(message)) {
            L.e(message);
        }
    }

    public List<MessageBean.Data> getDataList() {
        return dataList;
    }

    public ListAdapter getAdapter(Context context) {
        return quickAdapter = new QuickAdapter<MessageBean.Data>(context, R.layout.item_message) {
            @Override
            protected void convert(BaseAdapterHelper helper, MessageBean.Data item) {
                helper.setText(R.id.txtTitle, item.getNoticeTitle());
                TextView txtDetail = helper.getView(R.id.txtDetail);
                txtDetail.setText(Html.fromHtml(item.getNoticeContent()));
                if (item.getCreateTime() != null) {
                    try {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
                        Date date = df.parse(item.getCreateTime().substring(0, "yyyy-MM-dd HH:mm:ss".length()));
                        helper.setText(R.id.txtSendTime, new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                String type = item.getNoticeTypeId();
                if ("1".equals(type)) {
                    helper.setImageResource(R.id.ivType, R.drawable.i_ring);
                } else if ("2".equals(type)) {
                    helper.setImageResource(R.id.ivType, R.drawable.i_ring);
                } else if ("0".equals(type)) {
                    helper.setImageResource(R.id.ivType, R.drawable.i_ring);
                }
                ImageView ivType = helper.getView(R.id.ivType);
                if ("1".equals(item.getReadFlg())) {
                    helper.setVisible(R.id.ivPoint, false);
//                    new QBadgeView(context).bindTarget(ivType).setBadgeGravity(Gravity.TOP | Gravity.END)
//                            .setBadgeText(" ")
//                            .setGravityOffset(-2,-2, true)
//                            .setBadgeTextSize(8, true);
                } else {
                    helper.setVisible(R.id.ivPoint, true);
                }
            }
        };
    }
}
